package com.onec.service.route.service.impl;

import com.onec.service.api.enums.StatusEnum;
import com.onec.service.api.enums.error.BusinessErrorEnum;
import com.onec.service.api.exception.RouteException;
import com.onec.service.route.dao.BusinessInfoRepository;
import com.onec.service.route.entity.BusinessInfo;
import com.onec.service.route.service.BusinessInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author onec
 * @see
 */
@Service
public class BusinessInfoServiceImpl implements BusinessInfoService {

    @Autowired
    BusinessInfoRepository businessInfoRepository;

    /**
     * 验证当前业务是否可用：
     *
     * @param businessNo
     * @return
     */
    @Override
    public boolean isSuccessBusiness(String businessNo) {
        BusinessInfo businessInfo = findBusinessInfoNo(businessNo);
        if (!StatusEnum.isSuccess(businessInfo.getStatus())) {
            throw new RouteException(BusinessErrorEnum.BUSINESS_NOT_AVAILABLE, businessNo);
        }
        return true;
    }

    @Override
    public BusinessInfo findBusinessInfoNo(String businessNo) {
        BusinessInfo businessInfo = businessInfoRepository.findByCode(businessNo);
        if (Optional.ofNullable(businessInfo).isEmpty()) {
            throw new RouteException(BusinessErrorEnum.BUSINESS_NOT_EXIST, businessNo);
        }
        return businessInfo;
    }
}
